﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using RT = Coolzon.Metadata.Runtime;

namespace Coolzon.UI.Core.Converters
{
    /*
     * bool 
     * 和
     * <Bool Value="false"/>
     * 之间相互转换
     * */
    public class BoolConverter : ValueConverter
    {
        public object Convert(ConvertContext context, RT.Property property)
        {
            bool value = false;
            if(!string.IsNullOrEmpty(property.ValueString))
            {
                bool.TryParse(property.ValueString, out value);
                return value;
            }
            else
            {
                string valueString = property.ValueNode.GetAtrributeValue("Value");
                bool.TryParse(property.ValueString, out value);
                return value;
            }
        }

        public RT.ValueNode ConvertBack(ConvertContext context, object value, object dtTagData)
        {
            RT.ValueNode valueNode = new RT.ValueNode("Bool");
            valueNode.Attributes = new Dictionary<string, string>();
            valueNode.Attributes.Add("Value", System.Convert.ToString(value));
            return valueNode;
        }
    }
}
